package com.wiseMen.weixin.user;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wiseMen.platform.utils.AjaxInfo;
import com.wiseMen.platform.utils.AjaxUtils;
import com.wiseMen.platform.utils.RequestUtils;
import com.wiseMen.weixin.utils.SessionUtils;


@Controller
public class WxUserAction {
	
	@Autowired
	private WxUserService service;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@RequestMapping("/weixin/login")
	public String login(){
		//WxUserEntity user = null;
		WxUserEntity user = SessionUtils.getUser();
		if(user==null){
			String openId = SessionUtils.getOpenId();
			if(StringUtils.isBlank(openId)){
				openId = SessionUtils.refurbishOpenId(RequestUtils.getServletRequest().getParameter("code"));
			}
			if(StringUtils.isNotBlank(openId) && service.isRegister(openId)){
				user = service.findById(openId);
				if(user==null){
					return "weixin/register/bind";
				}
			}else{
				return "weixin/register/personal";
			}
		}
		RequestUtils.getServletRequest().setAttribute("user", user);
		SessionUtils.setUser(user);
		return "weixin/user/userCenter";
	}
	
	/**
	 * 跳转登录界面
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/weixin/bind")
	public String bind(String username,String password){
		return "weixin/register/bind";
	}
	
	/**
	 * 跳转个人信息详情页面
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/weixin/info")
	public String info(){
		WxUserEntity user = SessionUtils.getUser();
		if(user==null){
			String openId = SessionUtils.getOpenId();
			if(service.isRegister(openId)){
				user = service.findById(openId);
				if(user==null){
					return "weixin/register/bind";
				}
			}else{
				return "weixin/register/personal";
			}
		}
		RequestUtils.getServletRequest().setAttribute("user", user);
		if(StringUtils.equals("1", user.getRoleCode())){
			return "/weixin/user/info/enterprise";
		}else if(StringUtils.equals("3", user.getRoleCode())){
			return "/weixin/user/info/intermediary";
		}else{
			return "/weixin/user/info/personal";
		}
	}
	
	@RequestMapping("/weixin/bind/save")
	@ResponseBody
	public Object bindSave(String username,String password){
		try {
			String openId = SessionUtils.getOpenId();
			if(StringUtils.isNotBlank(openId)){
				WxUserEntity user = this.service.login(username, password);
				if(user==null){
					return AjaxUtils.renderJsonInfo(false,"用户名或者密码有误",null);
				}
				SessionUtils.setUser(user);
				this.service.bind(openId, username, password);
			}else{
				return AjaxUtils.renderJsonInfo(false,"微信公众号登录已经超时，请关闭页面重新进入",null);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			return AjaxUtils.renderJsonInfo(false,"当前操作出现异常，请稍后重试",null);
		}
		return AjaxUtils.renderJsonInfo(true,"登录成功",null);
	}
	
	/**
	 * 跳转到个人用户注册页面
	 * @return
	 */
	@RequestMapping("/weixin/personal")
	public String personal(){
		return "weixin/register/personal";
	}
	
	/**
	 * 个人用户注册
	 * @param username 用户名
	 * @param password 用户密码
	 * @param certificateNum 身份证号码
	 * @param email 电子邮箱
	 * @return
	 */
	@RequestMapping("/weixin/personal/register")
	@ResponseBody
	public Object personalRegister(String userid,String username,String password,String certificateNum,String email){
		AjaxInfo result = null;
		String openId = SessionUtils.getOpenId();
		if(StringUtils.isNotBlank(openId)){
			try {
				if(StringUtils.isBlank(userid) && service.isRegister(openId)){
					WxUserEntity user = service.findById(openId);
					if(user!=null){
						return AjaxUtils.renderJsonInfo(true,"注册成功",null);
					}
				}
				result = this.service.personalRegister(openId ,userid, username, password, certificateNum, email);
			} catch (Exception e) {
				result = AjaxUtils.renderJsonInfo(false,"用户注册时出现异常情况，请重新注册",null);
				e.printStackTrace();
			}
		}else{
			result = AjaxUtils.renderJsonInfo(false,"微信公众号登录已经超时，请关闭页面重新进入",null);
		}
		return result;
	}
	
	/**
	 * 跳转到企业用户注册页面
	 * @return
	 */
	@RequestMapping("/weixin/enterprise")
	public String enterprise(){
		return "weixin/register/enterprise";
	}
	
	/**
	 * 企业用户注册
	 * @param username
	 * @param password
	 * @param email
	 * @param orgName
	 * @param orgArea1
	 * @param orgArea2
	 * @param industry1
	 * @param industry2
	 * @param industry3
	 * @param industry4
	 * @return
	 */
	@RequestMapping("/weixin/enterprise/register")
	@ResponseBody
	public Object enterpriseRegister(String userid,String username,String orgId,String password,String email,String orgName
			,String orgArea1,String orgArea2,String industry1,String industry2,String industry3,String industry4){
		AjaxInfo result = null;
		String openId = SessionUtils.getOpenId();
		if(StringUtils.isNotBlank(openId)){
			try {
				if(StringUtils.isBlank(userid) && service.isRegister(openId)){
					WxUserEntity user = service.findById(openId);
					if(user!=null){
						return AjaxUtils.renderJsonInfo(true,"注册成功",null);
					}
				}
				result = this.service.enterpriseRegister(openId ,userid ,orgId ,username, password, email, orgName, 
						orgArea1, orgArea2, industry1, industry2, industry3, industry4);
			} catch (Exception e) {
				result = AjaxUtils.renderJsonInfo(false,"用户注册时出现异常情况，请重新注册",null);
				e.printStackTrace();
			}
		}else{
			result = AjaxUtils.renderJsonInfo(false,"微信公众号登录已经超时，请关闭页面重新进入",null);
		}
		return result;
	}
	
	@RequestMapping("/weixin/getLoginUserInfo")
	@ResponseBody
	public Object getLoginUserInfo(){
		WxUserEntity user = SessionUtils.getUser();
		if(user==null){
			String openId = SessionUtils.getOpenId();
			if(StringUtils.isNotBlank(openId)){
				if(this.service.isRegister(openId)){
					user = this.service.findById(openId);
					if(user==null){
						//用户账号密码有误，需要重新绑定账号
						return AjaxUtils.renderJsonInfo(false,"001",null);
					}
				}else{
					//未注册
					return AjaxUtils.renderJsonInfo(false,"002",null);
				}
			}else{
				//微信公众号登录已经超时，请关闭页面重新进入
				return AjaxUtils.renderJsonInfo(false,"000",null);
			}
		}
		return AjaxUtils.renderJsonInfo(true,"获取用户信息成功",user);
	}
	
	/**
	 * 输出响应
	 * 
	 * @param str
	 * @throws IOException
	 */
	protected void write(String str, HttpServletResponse response) throws IOException {
		response.getWriter().write(str);
		response.getWriter().flush();
		response.getWriter().close();
	}
	
}
